package com.carefree.lxwy.manager.controller;


import com.carefree.lxwy.manager.service.SysRoleService;
import com.carefree.lxwy.model.dto.system.SysRoleDto;
import com.carefree.lxwy.model.entity.system.SysRole;
import com.carefree.lxwy.model.vo.common.Result;
import com.carefree.lxwy.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import java.util.Map;


@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    // 删除
    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.deleteById(roleId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    // 修改
    @PutMapping(value = "/updateSysRole")
    public Result updateSysRole(@org.springframework.web.bind.annotation.RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    // 新增
    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@org.springframework.web.bind.annotation.RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 分页查询
    @PostMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@RequestBody SysRoleDto sysRoleDto ,
                                                @PathVariable(value = "pageNum") Integer pageNum ,
                                                @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto , pageNum , pageSize);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS);
    }

    // 查询所有角色
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String , Object>> findAllRoles(@PathVariable("userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS)  ;
    }

}
